;(function ($) {
    /*
    * 保养评分
    * */
    $.repairScore = function (dispatchId,id,assetsId,maintenanceType, system, isWebsocket, messageObj, isBatchDeal) {
        /* 添加模态框元素 */
        var model = $("#repairScore");
        if (!model.length) {
            $('body').append('<div id="repairScore" class="score" style="display:none">' +
                '<div class="box-body">' +
                    '<form class="form-horizontal engineerConfirmFrom" role="engineerConfirmFrom" method="post" enctype="multipart/form-data">' +
                        '<!-- (报修表id) -->' +
                        '<input type="hidden" class="confimrRepairId" name="repairId">				<!-- 维修处理ID -->' +
                        '<input type="hidden" class="confirmDispatchId" name = "dispatchId">' +
                        '<input type="hidden" class="userId" name = "userId" value="${loginUser.id}">			<!-- 设备ID -->' +
                        '<div class="form-group" style="padding-left: 0.3rem">' +
                            '<table class="grade-table" style="width: 100%">' +
                                '<tr>' +
                                    '<th>是否修复<span class="text-red">*</span></th>' +
                                    '<td><label><input type="radio" class="flat-green" checked="checked" value="3007" name="repairStatus">已修复</label></td>' +
                                    '<td><label><input type="radio" class="flat-green" name="repairStatus" value="3006">未修复</label></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>服务总体评分<span class="text-red">*</span></th>' +
                                    '<td colspan="5"><div class="scores totalScore" name="score"></div></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>服务响应速度<span class="text-red">*</span></th>' +
                                    '<td colspan="5"><div class="scores speedScore" name="speedScore"></div></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>服务质量<span class="text-red">*</span></th>' +
                                    '<td colspan="5"><div class="scores qualityScore" name="qualityScore"></div></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>服务态度<span class="text-red">*</span></th>' +
                                    '<td colspan="5"><div class="scores attitudeScore" name="attitudeScore"></div></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>维修状况<span class="text-red">*</span></th>' +
                                    '<td><label><input type="radio" class="flat-green" value="1" name="repairCondition">大修</label></td>' +
                                    '<td><label><input type="radio" class="flat-green"  value="2" name="repairCondition">中修</label></td>' +
                                    '<td><label><input type="radio" class="flat-green" checked="checked" value="3" name="repairCondition">小修</label></td>' +
                                '</tr>' +
                                '<tr>' +
                                '<th>备件费用</th>' +
                                    '<td colspan="5" style="padding-right: 0.7rem !important"><input class="form-control" type="text" name="partCost" class="partCost"></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>人工费用</th>' +
                                    '<td colspan="5" style="padding-right: 0.7rem !important"><input class="form-control" type="text" name="artificialCost" class="artificialCost"></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>快递费</th>' +
                                    '<td colspan="5" style="padding-right: 0.7rem !important"><input class="form-control" type="text" name="expressCost" class="expressCost"></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>总费用</th>' +
                                    '<td colspan="5" style="padding-right: 0.7rem !important"><input class="form-control" type="text" name="sumCost" class="sumCost"></td>' +
                                '</tr>' +
                                '<tr class="receive-tr hide">' +
                                    '<th>收到时间<span class="text-red">*</span></th>' +
                                    '<td colspan="5" style="padding-right: 0.7rem !important"><input class="form-control" type="text" name="hospitalReceivedDate" class="hospitalReceivedDate"></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<th>发表评价</th>' +
                                    '<td colspan="5" style="padding-right: 0.7rem !important"><input class="form-control" type="text" name="comment"></td>' +
                                '</tr>' +
                            '</table>' +
                        '</div>' +
                    '</form>' +
                '</div>' +
                '<div class="box-footer text-center">' +
                    '<button type="button" class="btn save" onclick="">保存</button>' +
                    '<button type="button" class="btn undo">撤回</button>' +
                    '<button class="btn btn-default cancel">取消</button>' +
                '</div>' +
            '</div>');
            model = $("#repairScore");
            if (system == 'report') {
                model.find('.undo').hide();
            }
            model.iziModal({
                title: "医院确认",
                headerColor: '#fff',
                radius: 0.4 * rem,
                overlayClose: false,
                theme: '',  // light
                autoOpen: false,
                zindex: 10010,
                width: 12 * rem,
                onOpening: function () {
                    model.find('input[type="radio"].flat-green').iCheck({
                        checkboxClass: 'icheckbox_flat-blue',
                        radioClass: 'iradio_flat-blue'
                    });
                },
                onClosed: function () {
                    model.find('.engineerConfirmFrom')[0].reset();
                    model.addClass('score');
                    model.find('.scores').raty('set', {score: 5});
                }
            });

            model.find('.scores').each(function () {
                $(this).raty({
                    path: basePath + '/plugins/raty/images',
                    hints: ['不满意','比较满意','一般','不太满意','满意'],
                    score: 5,
                    scoreName: $(this).attr('name')
                });
            });
        }
        /* 撤回 */
        model.find('.undo').off().click(function () {
            new $.flavr({
                closeOverlay : true,
                closeEsc : true,
                content : '确定撤回吗？',
                buttons:[
                    {text:'取消',style:'info',action:function(){}},
                    {
                        text:'确定',
                        style:'primary',
                        action:function(){
                            $.ajax({
                                url: basePath + "/dispatching/undo",
                                type:'POST',
                                data:{dispatchId:model.find(".confirmDispatchId").val()},
                                async : false,
                                success:function(data){
                                    if(data.ret == 1){
                                        model.iziModal('close',this);
                                        flavrShowByTime(data.msg,null,"success",null);
                                        location.reload();
                                    }else{
                                        flavrShowByTime(data.msg,null,"error",null);
                                    }
                                }
                            });
                        }
                    }
                ]
            });
        });
        /* 保存 */
        model.find('.save').off().click(function () {
            if(maintenanceType == "1" && model.find('.hospitalReceivedDate').val() == ''){
                flavrShowByTime("送回时间不能为空",null,"error");
                return;
            }else{
                var formData = new FormData(model.find(".engineerConfirmFrom")[0]);
                $.ajax({
                    type: "POST",
                    url: basePath + "/maintenance/update",
                    data: formData,
                    dataType : "json",
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(data){
                        if(data.ret){
                            flavrShowByTime("操作成功",null,"success",null);
                            if (isBatchDeal) {
                                dealBatchDeal();
                            } else if (isWebsocket) {
                                model.iziModal('close',this);
                                messageObjArr[messageObjArr.index].isDoNow = true;
                                dealPushMessage();

                                loading(function () {
                                    queryWarning();
                                    for(var i = 1; i < 8; i++) {
                                        queryWarningList(i);
                                    }
                                });
                            } else {
                                model.iziModal('close',this);
                            }
                        }else{
                            flavrShowByTime("操作失败",null,"error");
                        }
                    },
                    failure: function(data){
                        flavrShowByTime("操作失败",null,"error");
                    }
                })
            }
        });

        model.find('.cancel, .iziModal-button-close').off().click(function () {
            if (isWebsocket) {
                model.iziModal('close', this);
                messageObjArr[messageObjArr.index].isDoNow = true;
                dealPushMessage();
            }
        });
        model.find('.cancel').off().click(function () {
            if (isBatchDeal) {
                dealBatchDeal();
            } else {
                model.iziModal('close');
            }
        });
        if (messageObj) {
            model.iziModal('setTitle', '医院确认 ' + messageObj.assetsName + '(' + messageObj.department + ')');
        } else {
            model.iziModal('setTitle', '医院确认');
        }
        model.iziModal('open', this);

        if(maintenanceType == 1){
            model.find(".receive-tr").removeClass("hide");
        }else{
            model.find(".receive-tr").addClass("hide");
        }
        model.find(".confirmDispatchId").val(dispatchId);
        model.find(".confimrRepairId").val(id);
        model.find(".partCost").val("");			// 备件费用
        model.find(".artificialCost").val("");	// 人工费用
        model.find(".sumCost").val("");			// 总费用

        // 查询费用信息
        $.ajax({
            url: basePath + "/dispatching/getCost",
            type:'POST',
            data:{dispatchId:dispatchId},//整个表单提交
            async : true, 	//默认为true 异步
            success:function(data){
                if (data.ret) {
                    model.find(".partCost").val(data.data.partCost);		// 备件费用
                    model.find(".artificialCost").val(data.data.artificialCost);	// 人工费用
                    if ((data.data.partCost != null && data.data.partCost != "") && (data.data.artificialCost != null || data.data.artificialCost != "")) {
                        var sum = data.data.partCost + data.data.artificialCost;
                        model.find(".sumCost").val(sum);		// 总费用
                        if (data.data.assetsPrice != 0) {
                            model.find("input[name='repairCondition']:checked").parent("div").removeClass("checked");		// 清空单选款样式
                            var percent = sum / data.data.assetsPrice * 100;
                            if (percent >= 10) {
                                model.find("input[name='repairCondition'][value='1']").attr("checked","checked");			// 赋默认值
                            } else if (percent < 10 && percent > 5) {
                                model.find("input[name='repairCondition'][value='2']").attr("checked","checked");			// 赋默认值
                            }else{
                                model.find("input[name='repairCondition'][value='3']").attr("checked","checked");
                            }
                            model.find("input[name='repairCondition']:checked").parent("div").addClass("checked");			// 加载样式
                        }
                    }
                }
            }
        });
    };
})(jQuery);